package com.yxb.service.server.manager;

import com.allen.dao.GenericDao;
import com.yxb.service.common.domain.UserDevice;
import com.yxb.service.common.option.UserDeviceOption;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UserDeviceManager  {

    @Autowired
    private GenericDao dao;

    public UserDevice  addUserDevice(UserDevice userDevice){
        userDevice.setCreatedTime(LocalDateTime.now());
        userDevice.setUpdatedTime(LocalDateTime.now());
        int result = dao.insertAndSetupId("UserDeviceMapper.insert", userDevice);
        return result  == 1 ? userDevice : null ;
    }


    public UserDevice  updateUserDevice(UserDevice userDevice){
        userDevice.setUpdatedTime(LocalDateTime.now());
        int result = dao.updateByObj("UserDeviceMapper.updateByPrimaryKey", userDevice);
        return result  == 1 ? userDevice : null ;
    }


    public List<UserDevice>  queryList(UserDeviceOption userDeviceOption){

        Map<String, Object> param = new HashMap<>(5);

        if (StringUtils.isNotBlank(userDeviceOption.getDeviceId()) ){
            param.put("deviceId", userDeviceOption.getDeviceId());
        }

        if (StringUtils.isNotBlank(userDeviceOption.getUserId())){
            param.put("userId", userDeviceOption.getUserId());
        }
        return dao.queryList("UserDeviceMapper.selectByOption", param);
    }

    public UserDevice  queryById(Integer id){
        Map<String, Object> param = new HashMap<>(1);
        param.put("id", id);
        return dao.queryUnique("UserDeviceMapper.selectByPrimaryKey", param);
    }

    public UserDevice queryByDeviceId(String deviceId, boolean status){
        Map<String, Object> param = new HashMap<>(5);
        param.put("deviceId", deviceId);
        param.put("status", status);
        return dao.queryUnique("UserDeviceMapper.selectByOption", param);
    }



}
